/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示通过中断方式接收数据
  *          本样例演示通过中断方式发送数据
  *          需要在UART2的中断服务程序中调用HAL_UART_IRQHandler函数
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：断开J12
  *                    短接J13
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_uart.h"
#include "sym32l010_startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/
#define BUF_SIZE 500

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
UART_HandleTypeDef huart2;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void UART_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/
uint8_t TxRxBuf[BUF_SIZE];

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 初始化系统时钟
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化SysTick
    HAL_InitTick(48000000);

    // 初始化UART
    UART_Init();

    //------------------------------------------------------------
    // 请通过串口助手向MCU发送 BUF_SIZE 个字节
    // 请通过串口助手从MCU接收 BUF_SIZE 个字节
    while (1)
    {
        // 以下通过中断接收 BUF_SIZE 个字节
        HAL_UART_Receive_IT(&huart2, TxRxBuf, BUF_SIZE);
        while (HAL_UART_Receive_IT_IsComplete(&huart2) != HAL_OK);    // 等待接收完成

        // 以下通过中断发送 BUF_SIZE 个字节
        HAL_UART_Transmit_IT(&huart2, TxRxBuf, BUF_SIZE);
        while (HAL_UART_Transmit_IT_IsComplete(&huart2) != HAL_OK);   // 等待发送完成
    }
}


/*******************************************************************************
 * @brief  UART初始化
 * @param  None
 * @retval None
 */
static void UART_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));

    // 外设时钟使能
    BSP_UART_RXD_PORT_PERIPH_CLK_ENABLE();
    BSP_UART_TXD_PORT_PERIPH_CLK_ENABLE();
    BSP_UART_PERIPH_CLK_ENABLE();

    // 配置UART RXD/TXD管脚
    BSP_UART_TXD_AF();
    GPIO_InitStruct.Pins = BSP_UART_TXD_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(BSP_UART_TXD_PORT, &GPIO_InitStruct);

    BSP_UART_RXD_AF();
    GPIO_InitStruct.Pins = BSP_UART_RXD_PIN;
    GPIO_InitStruct.Pull = GPIO_PULL_UP;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    HAL_GPIO_Init(BSP_UART_RXD_PORT, &GPIO_InitStruct);

    // 配置 UART
    MemClr(&huart2, sizeof(huart2));

    huart2.Instance         = BSP_UART;
    huart2.RxDoneFun        = UART_RXDONE_FIXLEN;                     // 按照固定长度接收
    huart2.Init.ClockSource = UART_CLKSRC_PCLK;
    huart2.Init.PclkFreq    = 48000000;
    huart2.Init.BaudRate    = 115200;                                 // Baud = 115200
    huart2.Init.ParityMode  = UART_PARITYMODE_NONE;                   // 无校验位
    huart2.Init.StopBits    = UART_STOPBITS_1;                        // STOP = 1
    HAL_UART_Init(&huart2);                                           // 初始化UART

    HAL_UART_TXD_Enable(huart2.Instance);                             // 使能UART的TXD功能
    HAL_UART_RXD_Enable(huart2.Instance);                             // 使能UART的RXD功能

    NVIC_ClearPendingIRQ(BSP_UART_IRQn);
    NVIC_EnableIRQ(BSP_UART_IRQn);
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
